Page({
  data: {
    username: '小明',
    totalPoints: 280,
    levels: [
      {
        name: '萌芽新星',
        min: 0,
        max: 99,
        desc: '这个阶段是孩子们积分旅程的起点，他们开始了解并参与到积分活动中来。'
      },
      {
        name: '探索小勇士',
        min: 100,
        max: 299,
        desc: '孩子们开始积极地参与各种活动，探索未知，积累了一定的积分。'
      },
      {
        name: '智慧小博士',
        min: 300,
        max: 599,
        desc: '在这个阶段，孩子们展现出了较高的智慧和学习热情，他们的积分增长迅速。'
      },
      {
        name: '创意小天才',
        min: 600,
        max: 999,
        desc: '孩子们开始展现出非凡的创造力和想象力，他们的作品和想法常常令人眼前一亮。'
      },
      {
        name: '梦想领航员',
        min: 1000,
        max: 1999,
        desc: '孩子们已经拥有了明确的目标和梦想，他们正朝着这些目标努力前行。'
      },
      {
        name: '荣耀小英雄',
        min: 2000,
        max: 999999,
        desc: '这是积分机制的最高级别，只有那些持续努力、不断进步的孩子们才能达到。'
      }
    ],
    currentLevel: {},
    progressPercentage: 0,
    pointsToNextLevel: 0,
    version: '1.0.0'
  },

  onLoad() {
    this.calculateLevel();
  },

  calculateLevel() {
    const { totalPoints, levels } = this.data;
    let currentLevel = levels[0];
    let nextLevel = levels[1];

    // 计算当前等级
    for (let i = 0; i < levels.length; i++) {
      if (totalPoints >= levels[i].min && totalPoints <= levels[i].max) {
        currentLevel = levels[i];
        nextLevel = levels[i + 1];
        break;
      }
    }

    // 计算进度
    const levelRange = currentLevel.max - currentLevel.min;
    const currentProgress = totalPoints - currentLevel.min;
    const progressPercentage = (currentProgress / levelRange) * 100;

    // 计算距离下一级所需积分
    const pointsToNextLevel = nextLevel ? nextLevel.min - totalPoints : 0;

    this.setData({
      currentLevel,
      progressPercentage,
      pointsToNextLevel
    });
  },

  // 去登录
  goToLogin() {
    wx.navigateTo({
      url: '/pages/login/login'
    });
  },

  // 去注册
  goToRegister() {
    wx.navigateTo({
      url: '/pages/register/register'
    });
  },

  // 单项点击事件
  handleMenuClick(e) {
    const { type } = e.currentTarget.dataset;
    if (type === 'level-detail') {
      wx.navigateTo({
        url: '/pages/level-detail/level-detail'
      });
    }
    const routes = {
      'score-history': '/pages/score-history/score-history',
      'leaderboard': '/pages/leaderboard/leaderboard',
      homework: '/pages/homework/homework',
      invitation: '/pages/invitation/invitation',
      customer: '/pages/customer-service/customer-service',
      about: '/pages/about/about'
    };

    if (routes[type]) {
      wx.navigateTo({
        url: routes[type]
      });
    }
  },

  onShareAppMessage() {
    return {
      title: '一起来学英语吧！',
      path: '/pages/index/index?inviter=' + wx.getStorageSync('userId'),
      success: () => {
        // 邀请成功后加分
        getApp().addScore(30, 'invite_friend');
        wx.showToast({
          title: '邀请成功 +30分',
          icon: 'success'
        });
      }
    }
  }
}); 